#include <iostream>
#include <fstream>
#include <boost/iostreams/filtering_stream.hpp>
#include <alpha/rpc/bencode.hpp>
#include <alpha/crypto/hash/sha1.hpp>
#include <alpha/crypto/encoding/hex.hpp>

int main(int argc, char *argv[])
{
	if (argc != 2)
	{
		std::cout << "Usage: infohash FILE" << std::endl;
		return 1;
	}
	namespace io = boost::iostreams;
	typedef alpha::rpc::bencode::dictionary dictionary_t;
	std::ifstream in(argv[1], std::ios::in | std::ios::binary);
	alpha::rpc::bencode v;
	in >> v;
	if (!in || !v.pointer<dictionary_t>())
	{
		std::cerr << "Invalid file " << argv[1] << std::endl;
		return -1;
	}
	io::filtering_ostream out(alpha::crypto::sha1()
		| alpha::crypto::hex_encoder()
		| std::ostream_iterator<char>(std::cout));
	out << v.reference<dictionary_t>()["info"];
	std::cout << "infohash: ";
	out.reset();
	std::cout << std::endl;
}
